package com.runen.infinitemovement.activity;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.model.Text;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.tool.AppUtils;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.widget.ViewPagerAdapter;
import com.runen.rxnetlibrary.utils.SharePreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.pointLayout)
    LinearLayout vg;
    @BindView(R.id.tv_skip)
    TextView tvSkip;
    @BindView(R.id.btnStart)
    TextView btnStart;
    //图片资源的数组
    int[] imageIdArray = new int[]{R.mipmap.view_guide1, R.mipmap.ico_guide2, R.mipmap.ico_guide3};
    private List<View> viewList;//图片资源的集合
    //实例化原点View
    private ImageView iv_point;
    private ImageView[] ivPointArray;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        SharePreferenceUtils.putString(this, "WELCOME", "welcome");
        //加载ViewPager
        initViewPager();
        //加载底部圆点
        initPoint();
        tvSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserAPI.checkLogin()) {
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity2.class));
                } else {
//                    startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                    AppUtils.jumpToMain(WelcomeActivity.this);
                }
                finish();
            }
        });
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserAPI.checkLogin()) {
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity2.class));
                } else {
//                    startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                    AppUtils.jumpToMain(WelcomeActivity.this);
                }
                finish();
            }
        });
    }

    private void initViewPager() {
        viewList = new ArrayList<>();
        //获取一个Layout参数，设置为全屏
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建View并加入到集合中
        int len = imageIdArray.length;
        for (int i = 0; i < len; i++) {
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageDrawable(CommonUtil.getDrawable(imageIdArray[i]));

            //将ImageView加入到集合中
            viewList.add(imageView);
        }

        //View集合初始化好后，设置Adapter
        viewPager.setAdapter(new ViewPagerAdapter(viewList));
        //设置滑动监听
        viewPager.addOnPageChangeListener(this);
    }

    int dimen = CommonUtil.getDimens(R.dimen.dp_5);

    /**
     * 加载底部圆点
     */
    private void initPoint() {
        //这里实例化LinearLayout
        //根据ViewPager的item数量实例化数组
        ivPointArray = new ImageView[viewList.size()];
        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
        int size = viewList.size();
        for (int i = 0; i < size; i++) {
            iv_point = new ImageView(this);
            iv_point.setPadding(dimen, 0, dimen, 0);//left,top,right,bottom
            ivPointArray[i] = iv_point;
            //第一个页面需要设置为选中状态，这里采用两张不同的图片
            if (i == 0) {
                iv_point.setImageResource(R.drawable.bg_ff63e5a_360dp_size_7dp);
            } else {
                iv_point.setImageResource(R.drawable.bg_ffd6d7d8_cr_360dp);
            }
            //将数组中的ImageView加入到ViewGroup
            vg.addView(ivPointArray[i]);
        }


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //循环设置当前页的标记图
        int length = imageIdArray.length;
        for (int i = 0; i < length; i++) {
            ivPointArray[position].setImageResource(R.drawable.bg_ff63e5a_360dp_size_7dp);
            if (position != i) {
                ivPointArray[i].setImageResource(R.drawable.bg_ffd6d7d8_cr_360dp);
            }
        }

        //判断是否是最后一页，若是则显示按钮
        if (position == imageIdArray.length - 1) {
            btnStart.setVisibility(View.VISIBLE);
        } else {
            btnStart.setVisibility(View.GONE);
        }
    }

    private void showPopup() {


    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}